package org.lib.mysqlhello.tcache;

import java.util.Collection;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import lombok.extern.slf4j.Slf4j;

@RestController
@RequestMapping(value="/cacheManager")
@Slf4j
public class CacheManagerController {

	@Autowired
	private CacheManager cacheManager;
	
	@GetMapping(value="/test")
	public Boolean test() {
		log.info("cacheManager检查：cacheManager={}, cacheManager", 
				cacheManager.getClass(), cacheManager);
		Collection<String> names = cacheManager.getCacheNames();
		log.info("cacheNames={}", names);
		names.forEach(name->{
			log.info("name={}, value={}", name, cacheManager.getCache(name));
			
			Cache cache = cacheManager.getCache(name);
			log.info("cache.name={}", cache.getName());
			log.info("cache.get={}", cache.get(name));
			log.info("cache.getNativeCache={}", cache.getNativeCache());
		});
		
		// 输出 RedisCacheManager 的配置
		if (RedisCacheManager.class.equals(cacheManager.getClass())) {
			log.info("RedisCacheManager探究：");
			RedisCacheManager rcm = (RedisCacheManager) cacheManager;
			
			Map<String, RedisCacheConfiguration> cconfig = rcm.getCacheConfigurations();
			cconfig.forEach((key, val)->{
				log.info("key={}, value={}", key, val);
				log.info("val-5: {}, {}, {}, {}, {}", val.getAllowCacheNullValues(), 
						val.getKeyPrefix().orElse(""),
						val.getTtl(), 
						val.getKeySerializationPair(),
						val.getValueSerializationPair());
			});
		}
		
		return true;
	}
	
}
